package cn.chenfei.jfx;

import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.Tooltip;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.util.Duration;
import org.kordamp.ikonli.antdesignicons.AntDesignIconsFilled;
import org.kordamp.ikonli.antdesignicons.AntDesignIconsOutlined;
import org.kordamp.ikonli.javafx.FontIcon;

import java.lang.reflect.InvocationTargetException;


/**
 * @author ChenFei
 * @date 2022年4月16日11:08:24
 */
public class ButtonGroup extends HBox {

    private static final String[] LoginArr = {"Login_1", "Login_2", "Login_3", "Login_4", "Login_5"};
    private static Stage currentStage = null;
    private static int currentIndex = 0;

    private Button leftBut = new Button();
    private Button centerBut = new Button();
    private Button rightBut = new Button();

    {
        this.getStylesheets().add(App.class.getResource("css/button-group.css").toExternalForm());
        this.getChildren().addAll(leftBut, centerBut, rightBut);
        //
        leftBut.setGraphic(new FontIcon(AntDesignIconsFilled.CARET_LEFT));
        centerBut.setGraphic(new FontIcon(AntDesignIconsOutlined.CLOSE));
        rightBut.setGraphic(new FontIcon(AntDesignIconsFilled.CARET_RIGHT));

        //
        leftBut.getStyleClass().add("left-but");
        centerBut.getStyleClass().add("center-but");
        rightBut.getStyleClass().add("right-but");
        this.getStyleClass().add("but-group");
        //
        Tooltip left = new Tooltip("上一个");
        left.setShowDelay(Duration.millis(500));
        Tooltip center = new Tooltip("关闭");
        left.setShowDelay(Duration.millis(500));
        Tooltip right = new Tooltip("下一个");
        left.setShowDelay(Duration.millis(500));
        leftBut.setTooltip(left);
        centerBut.setTooltip(center);
        rightBut.setTooltip(right);
        //关闭程序
        centerBut.setOnMouseClicked(event -> {
            Button source = (Button) event.getSource();
            source.getParent().getScene().getWindow().hide();
        });
        //上一个
        leftBut.setOnMouseClicked(event -> {
            currentStage.close();
            if (currentIndex <= 0) {
                currentIndex = LoginArr.length;
            }
            currentIndex--;
            Object loginInstance = getLoginInstance();
            currentStage = showLoginStage(loginInstance);
            currentStage.show();
        });
        //下一个
        rightBut.setOnMouseClicked(event -> {
            currentStage.close();
            if (currentIndex >= LoginArr.length - 1) {
                currentIndex = -1;
            }
            currentIndex++;
            Object loginInstance = getLoginInstance();
            currentStage = showLoginStage(loginInstance);
            currentStage.show();
        });
        //打开第一个登录窗口
        Object loginInstance = getLoginInstance();
        currentStage = showLoginStage(loginInstance);
        currentStage.show();
    }

    /**
     * 通过反射获取到登录组件对象
     *
     * @return
     */
    private Object getLoginInstance() {
        try {
            Class<?> clazz = Class.forName("cn.chenfei.jfx.login." + LoginArr[currentIndex]);
            Object o = clazz.getDeclaredConstructor().newInstance();
            return o;
        } catch (ClassNotFoundException | NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 打开窗口
     *
     * @param obj
     * @return
     */
    private Stage showLoginStage(Object obj) {
        Stage stage = new Stage();
        Scene scene = new Scene((Parent) obj);
        scene.setFill(null);
        stage.initStyle(StageStyle.TRANSPARENT);
        stage.setScene(scene);
        stage.setResizable(false);
        stage.show();
        return stage;
    }

}
